package com.bjx.generator.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bjx.generator.domain.entity.TableFieldEntity;
import com.bjx.generator.domain.entity.TableInfoEntity;
import com.bjx.generator.domain.query.TableInfoQuery;
import com.bjx.generator.domain.vo.TableInfoVO;
import com.bjx.generator.domain.vo.TableSonVO;
import com.bjx.generator.mapper.TableInfoMapper;
import com.bjx.generator.service.IDatabaseService;
import com.bjx.generator.service.ITableFieldService;
import com.bjx.generator.service.ITableInfoService;
import com.bjx.generator.service.ITableSonService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @desc: 表信息业务
 * @author: liushuai
 * @date: 2022/9/14
 */
@Service
public class TableInfoServiceImpl extends ServiceImpl<TableInfoMapper, TableInfoEntity> implements ITableInfoService {
    @Resource(name = "postgreSqlDatabaseServiceImpl")
    private IDatabaseService databaseService;
    @Resource
    private ITableFieldService tableFieldService;

    @Resource
    private TableInfoMapper tableInfoMapper;

    @Resource
    private ITableSonService tableSonService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void insertTableInfo(String tableName) {
        String className = StrUtil.upperFirst(StrUtil.toCamelCase(tableName));
        String comment = databaseService.selectTableComment(tableName);
        TableInfoEntity entity = TableInfoEntity.builder()
                .className(className)
                .descName(comment)
                .tableName(tableName)
                .build();
        this.save(entity);
        List<TableFieldEntity> entityList = databaseService.selectTableFieldByTableName(tableName);
        tableFieldService.saveTableFieldBatchByTableId(entityList, entity.getTableId());
    }

    @Override
    public IPage<TableInfoEntity> listTableInfoPage(TableInfoQuery tableInfoQuery) {
        LambdaQueryWrapper<TableInfoEntity> wrapper = new LambdaQueryWrapper();
        wrapper.like(StrUtil.isNotEmpty(tableInfoQuery.getTableName()), TableInfoEntity::getTableName, tableInfoQuery.getTableName());
        IPage<TableInfoEntity> page = tableInfoMapper.selectPage(tableInfoQuery.getPage(), wrapper);
        return page;
    }

    @Override
    public TableInfoVO selectTableInfoByTableId(Long tableId) {
        TableInfoEntity entity = tableInfoMapper.selectById(tableId);
        TableInfoVO tableInfo = Convert.convert(TableInfoVO.class, entity);
        List<TableFieldEntity> entityList = tableFieldService.listTableFieldByTableId(entity.getTableId());
        tableInfo.setFields(entityList);
        List<TableSonVO> tableSonEntities = tableSonService.listTableSonByTableId(entity.getTableId());
        tableInfo.setTableSons(tableSonEntities);
        return tableInfo;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateTableInfoByTableId(TableInfoVO vo) {
        TableInfoEntity entity = Convert.convert(TableInfoEntity.class, vo);
        tableInfoMapper.updateById(entity);
        tableFieldService.saveTableFieldBatchByTableId(vo.getFields(), entity.getTableId());
        tableSonService.saveTableSonBatchByTableId(vo.getTableSons(),entity.getTableId());
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteTableInfoByTableId(Long tableId) {
        tableInfoMapper.deleteById(tableId);
        tableSonService.deleteTableSonByTableId(tableId);
        tableFieldService.deleteTableFieldByTableId(tableId);
    }

    @Override
    public List<TableInfoEntity> listTableInfo(TableInfoQuery tableInfoQuery) {
        LambdaQueryWrapper<TableInfoEntity> wrapper = new LambdaQueryWrapper();
        wrapper.like(StrUtil.isNotEmpty(tableInfoQuery.getTableName()),TableInfoEntity::getTableName,tableInfoQuery.getTableName());
        return tableInfoMapper.selectList(wrapper);
    }


}
